package biz.inv.service;


import biz.inv.dto.Balance;
import biz.inv.dto.StockOut;
import biz.inv.dto.StockOutDetail;
import biz.inv.dto.StockTransSearch;
import glen.util.TemplateDAO;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class StockOutSvr extends TemplateDAO<StockOut, Integer> {

    public StockOutSvr() {
        super(StockOut.class);

    }

    public StockOutSvr(Class<StockOut> type) {
        super(type);

    }

    public StockOutSvr(Class<StockOut> type, SqlSessionFactory sf) {
        super(type, sf);

    }

    public List<StockOut> search(StockTransSearch o) throws PersistenceException {


        List<StockOut> list;
        try (SqlSession session = sf.openSession()) {
            String query = "StockOut.search";

            list = session.selectList(query, o);

        }
        return list;
    }

    public int delete(int id) {

        try (SqlSession session = sf.openSession()) {

            List<StockOutDetail> details = session.selectList("StockOut.searchStockOutDetail", id);
            for (StockOutDetail detail : details) {
                session.update("Balance.deleteStockDetail", detail);
            }
            session.delete("StockOut.delete", id);
            session.delete("StockOut.deleteStockOutDetail", id);

            session.commit();

        }
        return 0;
    }

    public int save(StockOut stockOut, List<StockOutDetail> stockOutDetails) {

        boolean isUpdate = (stockOut.getId() == 0);
        try (SqlSession session = sf.openSession()) {
            if (isUpdate) {
                session.insert("StockOut.create", stockOut);
            } else {
                session.update("StockOut.update", stockOut);
                //update Balance by sub Old StockIn Detail Qty
                List<StockOutDetail> oldDetails = session.selectList("StockOut.searchStockOutDetail", stockOut.getId());
                for (StockOutDetail detail : oldDetails) {
                    session.update("Balance.update", new Balance(detail.getProduct().getId(), detail.getQty()));
                }
                session.delete("StockOut.deleteStockOutDetail", stockOut.getId());
            }

            for (StockOutDetail detail : stockOutDetails) {

                detail.setReleaseId(stockOut.getId());
                session.insert("StockOut.createStockOutDetail", detail);

                Balance balance = session.selectOne("Balance.get", detail.getProduct().getId()); //Check Existing Balance

                if (balance == null) {
                    balance = new Balance(detail.getProduct().getId(), detail.getQty() * -1);
                    session.insert("Balance.create", balance);
                } else {
                    balance.setQty(detail.getQty() * -1);
                    session.update("Balance.update", balance);
                }
            }

            session.commit();
        }
        return 0;
    }

    public List<StockOutDetail> searchReleaseDetail(Integer id) {

        List<StockOutDetail> list;
        try (SqlSession session = sf.openSession()) {
            list = session.selectList("StockOut.searchStockOutDetail", id);
        }
        return list;
    }

}
